package com.lcq.springbootcreate.project.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lcq.springbootcreate.project.ExceptionAndEnum.MallException;
import com.lcq.springbootcreate.project.ExceptionAndEnum.MallExceptionEnum;
import com.lcq.springbootcreate.project.dao.CategoryDAO;
import com.lcq.springbootcreate.project.entity.Category;
import com.lcq.springbootcreate.project.request.AddCategoryReq;
import com.lcq.springbootcreate.project.request.UpdateCategoryReq;
import com.lcq.springbootcreate.project.vo.CategoryVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class CategroryService {
    @Autowired
    CategoryDAO categoryDAO;

    public void addCategory(AddCategoryReq addCategoryReq) throws MallException {
        Category category = new Category();
        // 把第一个属性复制到第二个当中
        BeanUtils.copyProperties(addCategoryReq, category);
        Category categoryOld = categoryDAO.selectByName(addCategoryReq);
        if (categoryOld != null) {
            throw new MallException(MallExceptionEnum.NAME_EXISTS);
        }
        // 商品插入
        int count = categoryDAO.insertByCategoryReq(addCategoryReq);
        if (count == 0) {
            throw new MallException(MallExceptionEnum.INSERT_FAILED);
        }
    }

    public void updateCategory(UpdateCategoryReq updateCategoryReq) {
        Category categoryOld = new Category();
        categoryOld = categoryDAO.selectByCategoryName(updateCategoryReq.getName());
        if (!updateCategoryReq.getId().equals(categoryOld.getId())) {
            throw new MallException(MallExceptionEnum.ARGUMENT_WRONG);
        }
        int count = categoryDAO.updateByCategory(updateCategoryReq);
        if (count == 0) {
            throw new MallException(MallExceptionEnum.UPDATE_FAILED);
        }
        return;
    }

    public void deleteById(Integer id) {
        categoryDAO.deleteById(id);
        return;
    }

    public PageInfo listForAdmin(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize, "type,order_num");
        List<CategoryVO> listAll = categoryDAO.getListAll();
        PageInfo pageInfo = new PageInfo(listAll);
        return pageInfo;
    }

    public List<CategoryVO> listForCustomer() {
        System.out.println("生成树方法");
        ArrayList<CategoryVO> categoryVOS = new ArrayList<>();
        recursivelyFindCategory(categoryVOS, 0);
        return categoryVOS;

    }

    private void recursivelyFindCategory(List<CategoryVO> categoryVOS, Integer parentId) {
        List<Category> listByParentId = categoryDAO.getListByParentId(parentId);
        System.out.println(listByParentId.toString());
        if (!CollectionUtils.isEmpty(listByParentId)) {
            for (int i = 0; i < listByParentId.size(); i++) {
                Category category = listByParentId.get(i);
                CategoryVO categoryVO = new CategoryVO();
                BeanUtils.copyProperties(category, categoryVO);
                categoryVOS.add(categoryVO);
                // 现在孩子list为空，父节点就是自己的Id
                recursivelyFindCategory(categoryVO.getChildCategory(), categoryVO.getId());
            }
        }
    }
}
